/*
 * Created by the DBAccessor generator.
 * Programmer: Lennart Martens
 * Date: 24/01/2008
 * Time: 16:17:53
 */
package org.hkupp.db.accessors;

import be.proteomics.util.db.interfaces.Deleteable;
import be.proteomics.util.db.interfaces.Persistable;
import be.proteomics.util.db.interfaces.Retrievable;
import be.proteomics.util.db.interfaces.Updateable;

import java.sql.*;
import java.util.HashMap;

/*
 * CVS information:
 *
 * $Revision: 1.4 $
 * $Date: 2007/07/06 09:41:53 $
 */

/**
 * This class is a generated accessor for the Protein table.
 *
 * @author DBAccessor generator class (Lennart Martens).
 */
public class ProteinTableAccessor implements Deleteable, Retrievable, Updateable, Persistable {

	/**
	 * This variable tracks changes to the object.
	 */
	protected boolean iUpdated = false;

	/**
	 * This variable can hold generated primary key columns.
	 */
	protected Object[] iKeys = null;

	/**
	 * This variable represents the contents for the 'proteinid' column.
	 */
	protected long iProteinid = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'accession_number' column.
	 */
	protected String iAccession_number = null;


	/**
	 * This variable represents the contents for the 'entry_name' column.
	 */
	protected String iEntry_name = null;


	/**
	 * This variable represents the contents for the 'database_name' column.
	 */
	protected String iDatabase_name = null;


	/**
	 * This variable represents the contents for the 'species' column.
	 */
	protected String iSpecies = null;


	/**
	 * This variable represents the contents for the 'protein_pI' column.
	 */
	protected Number iProtein_pi = null;


	/**
	 * This variable represents the contents for the 'protein_mw' column.
	 */
	protected Number iProtein_mw = null;


	/**
	 * This variable represents the contents for the 'proteinlength' column.
	 */
	protected long iProteinlength = Long.MIN_VALUE;


	/**
	 * This variable represents the contents for the 'creationdate' column.
	 */
	protected java.sql.Timestamp iCreationdate = null;


	/**
	 * This variable represents the contents for the 'modificationdate' column.
	 */
	protected java.sql.Timestamp iModificationdate = null;


	/**
	 * This variable represents the contents for the 'username' column.
	 */
	protected String iUsername = null;


	/**
	 * This variable represents the key for the 'proteinid' column.
	 */
	public static final String PROTEINID = "PROTEINID";

	/**
	 * This variable represents the key for the 'accession_number' column.
	 */
	public static final String ACCESSION_NUMBER = "ACCESSION_NUMBER";

	/**
	 * This variable represents the key for the 'entry_name' column.
	 */
	public static final String ENTRY_NAME = "ENTRY_NAME";

	/**
	 * This variable represents the key for the 'database_name' column.
	 */
	public static final String DATABASE_NAME = "DATABASE_NAME";

	/**
	 * This variable represents the key for the 'species' column.
	 */
	public static final String SPECIES = "SPECIES";

	/**
	 * This variable represents the key for the 'protein_pI' column.
	 */
	public static final String PROTEIN_PI = "PROTEIN_PI";

	/**
	 * This variable represents the key for the 'protein_mw' column.
	 */
	public static final String PROTEIN_MW = "PROTEIN_MW";

	/**
	 * This variable represents the key for the 'proteinlength' column.
	 */
	public static final String PROTEINLENGTH = "PROTEINLENGTH";

	/**
	 * This variable represents the key for the 'creationdate' column.
	 */
	public static final String CREATIONDATE = "CREATIONDATE";

	/**
	 * This variable represents the key for the 'modificationdate' column.
	 */
	public static final String MODIFICATIONDATE = "MODIFICATIONDATE";

	/**
	 * This variable represents the key for the 'username' column.
	 */
	public static final String USERNAME = "USERNAME";




	/**
	 * Default constructor.
	 */
	public ProteinTableAccessor() {
	}

	/**
	 * This constructor allows the creation of the 'ProteinTableAccessor' object based on a set of values in the HashMap.
	 *
	 * @param	aParams	HashMap with the parameters to initialize this object with.
	 *		<i>Please use only constants defined on this class as keys in the HashMap!</i>
	 */
	public ProteinTableAccessor(HashMap aParams) {
		if(aParams.containsKey(PROTEINID)) {
			this.iProteinid = ((Long)aParams.get(PROTEINID)).longValue();
		}
		if(aParams.containsKey(ACCESSION_NUMBER)) {
			this.iAccession_number = (String)aParams.get(ACCESSION_NUMBER);
		}
		if(aParams.containsKey(ENTRY_NAME)) {
			this.iEntry_name = (String)aParams.get(ENTRY_NAME);
		}
		if(aParams.containsKey(DATABASE_NAME)) {
			this.iDatabase_name = (String)aParams.get(DATABASE_NAME);
		}
		if(aParams.containsKey(SPECIES)) {
			this.iSpecies = (String)aParams.get(SPECIES);
		}
		if(aParams.containsKey(PROTEIN_PI)) {
			this.iProtein_pi = (Number)aParams.get(PROTEIN_PI);
		}
		if(aParams.containsKey(PROTEIN_MW)) {
			this.iProtein_mw = (Number)aParams.get(PROTEIN_MW);
		}
		if(aParams.containsKey(PROTEINLENGTH)) {
			this.iProteinlength = ((Long)aParams.get(PROTEINLENGTH)).longValue();
		}
		if(aParams.containsKey(CREATIONDATE)) {
			this.iCreationdate = (java.sql.Timestamp)aParams.get(CREATIONDATE);
		}
		if(aParams.containsKey(MODIFICATIONDATE)) {
			this.iModificationdate = (java.sql.Timestamp)aParams.get(MODIFICATIONDATE);
		}
		if(aParams.containsKey(USERNAME)) {
			this.iUsername = (String)aParams.get(USERNAME);
		}
		this.iUpdated = true;
	}


	/**
	 * This constructor allows the creation of the 'ProteinTableAccessor' object based on a resultset
	 * obtained by a 'select * from Protein' query.
	 *
	 * @param	aResultSet	ResultSet with the required columns to initialize this object with.
	 * @exception	SQLException	when the ResultSet could not be read.
	 */
	public ProteinTableAccessor(ResultSet aResultSet) throws SQLException {
		this.iProteinid = aResultSet.getLong("proteinid");
		this.iAccession_number = (String)aResultSet.getObject("accession_number");
		this.iEntry_name = (String)aResultSet.getObject("entry_name");
		this.iDatabase_name = (String)aResultSet.getObject("database_name");
		this.iSpecies = (String)aResultSet.getObject("species");
		this.iProtein_pi = (Number)aResultSet.getObject("protein_pI");
		this.iProtein_mw = (Number)aResultSet.getObject("protein_mw");
		this.iProteinlength = aResultSet.getLong("proteinlength");
		this.iCreationdate = (java.sql.Timestamp)aResultSet.getObject("creationdate");
		this.iModificationdate = (java.sql.Timestamp)aResultSet.getObject("modificationdate");
		this.iUsername = (String)aResultSet.getObject("username");

		this.iUpdated = true;
	}


	/**
	 * This method returns the value for the 'Proteinid' column
	 * 
	 * @return	long	with the value for the Proteinid column.
	 */
	public long getProteinid() {
		return this.iProteinid;
	}

	/**
	 * This method returns the value for the 'Accession_number' column
	 * 
	 * @return	String	with the value for the Accession_number column.
	 */
	public String getAccession_number() {
		return this.iAccession_number;
	}

	/**
	 * This method returns the value for the 'Entry_name' column
	 * 
	 * @return	String	with the value for the Entry_name column.
	 */
	public String getEntry_name() {
		return this.iEntry_name;
	}

	/**
	 * This method returns the value for the 'Database_name' column
	 * 
	 * @return	String	with the value for the Database_name column.
	 */
	public String getDatabase_name() {
		return this.iDatabase_name;
	}

	/**
	 * This method returns the value for the 'Species' column
	 * 
	 * @return	String	with the value for the Species column.
	 */
	public String getSpecies() {
		return this.iSpecies;
	}

	/**
	 * This method returns the value for the 'Protein_pi' column
	 * 
	 * @return	Number	with the value for the Protein_pi column.
	 */
	public Number getProtein_pi() {
		return this.iProtein_pi;
	}

	/**
	 * This method returns the value for the 'Protein_mw' column
	 * 
	 * @return	Number	with the value for the Protein_mw column.
	 */
	public Number getProtein_mw() {
		return this.iProtein_mw;
	}

	/**
	 * This method returns the value for the 'Proteinlength' column
	 * 
	 * @return	long	with the value for the Proteinlength column.
	 */
	public long getProteinlength() {
		return this.iProteinlength;
	}

	/**
	 * This method returns the value for the 'Creationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Creationdate column.
	 */
	public java.sql.Timestamp getCreationdate() {
		return this.iCreationdate;
	}

	/**
	 * This method returns the value for the 'Modificationdate' column
	 * 
	 * @return	java.sql.Timestamp	with the value for the Modificationdate column.
	 */
	public java.sql.Timestamp getModificationdate() {
		return this.iModificationdate;
	}

	/**
	 * This method returns the value for the 'Username' column
	 * 
	 * @return	String	with the value for the Username column.
	 */
	public String getUsername() {
		return this.iUsername;
	}

	/**
	 * This method sets the value for the 'Proteinid' column
	 * 
	 * @param	aProteinid	long with the value for the Proteinid column.
	 */
	public void setProteinid(long aProteinid) {
		this.iProteinid = aProteinid;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Accession_number' column
	 * 
	 * @param	aAccession_number	String with the value for the Accession_number column.
	 */
	public void setAccession_number(String aAccession_number) {
		this.iAccession_number = aAccession_number;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Entry_name' column
	 * 
	 * @param	aEntry_name	String with the value for the Entry_name column.
	 */
	public void setEntry_name(String aEntry_name) {
		this.iEntry_name = aEntry_name;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Database_name' column
	 * 
	 * @param	aDatabase_name	String with the value for the Database_name column.
	 */
	public void setDatabase_name(String aDatabase_name) {
		this.iDatabase_name = aDatabase_name;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Species' column
	 * 
	 * @param	aSpecies	String with the value for the Species column.
	 */
	public void setSpecies(String aSpecies) {
		this.iSpecies = aSpecies;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Protein_pi' column
	 * 
	 * @param	aProtein_pi	Number with the value for the Protein_pi column.
	 */
	public void setProtein_pi(Number aProtein_pi) {
		this.iProtein_pi = aProtein_pi;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Protein_mw' column
	 * 
	 * @param	aProtein_mw	Number with the value for the Protein_mw column.
	 */
	public void setProtein_mw(Number aProtein_mw) {
		this.iProtein_mw = aProtein_mw;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Proteinlength' column
	 * 
	 * @param	aProteinlength	long with the value for the Proteinlength column.
	 */
	public void setProteinlength(long aProteinlength) {
		this.iProteinlength = aProteinlength;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Creationdate' column
	 * 
	 * @param	aCreationdate	java.sql.Timestamp with the value for the Creationdate column.
	 */
	public void setCreationdate(java.sql.Timestamp aCreationdate) {
		this.iCreationdate = aCreationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Modificationdate' column
	 * 
	 * @param	aModificationdate	java.sql.Timestamp with the value for the Modificationdate column.
	 */
	public void setModificationdate(java.sql.Timestamp aModificationdate) {
		this.iModificationdate = aModificationdate;
		this.iUpdated = true;
	}

	/**
	 * This method sets the value for the 'Username' column
	 * 
	 * @param	aUsername	String with the value for the Username column.
	 */
	public void setUsername(String aUsername) {
		this.iUsername = aUsername;
		this.iUpdated = true;
	}



	/**
	 * This method allows the caller to delete the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int delete(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("DELETE FROM protein WHERE proteinid = ?");
		lStat.setLong(1, iProteinid);
		int result = lStat.executeUpdate();
		lStat.close();
		return result;
	}


	/**
	 * This method allows the caller to read data for this
	 * object from a persistent store based on the specified keys.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public void retrieve(Connection aConn, HashMap aKeys) throws SQLException {
		// First check to see whether all PK fields are present.
		if(!aKeys.containsKey(PROTEINID)) {
			throw new IllegalArgumentException("Primary key field 'PROTEINID' is missing in HashMap!");
		} else {
			iProteinid = ((Long)aKeys.get(PROTEINID)).longValue()
;		}
		// In getting here, we probably have all we need to continue. So let's...
		PreparedStatement lStat = aConn.prepareStatement("SELECT * FROM protein WHERE proteinid = ?");
		lStat.setLong(1, iProteinid);
		ResultSet lRS = lStat.executeQuery();
		int hits = 0;
		while(lRS.next()) {
			hits++;
			iProteinid = lRS.getLong("proteinid");
			iAccession_number = (String)lRS.getObject("accession_number");
			iEntry_name = (String)lRS.getObject("entry_name");
			iDatabase_name = (String)lRS.getObject("database_name");
			iSpecies = (String)lRS.getObject("species");
			iProtein_pi = (Number)lRS.getObject("protein_pI");
			iProtein_mw = (Number)lRS.getObject("protein_mw");
			iProteinlength = lRS.getLong("proteinlength");
			iCreationdate = (java.sql.Timestamp)lRS.getObject("creationdate");
			iModificationdate = (java.sql.Timestamp)lRS.getObject("modificationdate");
			iUsername = (String)lRS.getObject("username");
		}
		lRS.close();
		lStat.close();
		if(hits>1) {
			throw new SQLException("More than one hit found for the specified primary keys in the 'protein' table! Object is initialized to last row returned.");
		} else if(hits == 0) {
			throw new SQLException("No hits found for the specified primary keys in the 'protein' table! Object is not initialized correctly!");
		}
	}


	/**
	 * This method allows the caller to update the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int update(Connection aConn) throws SQLException {
		if(!this.iUpdated) {
			return 0;
		}
		PreparedStatement lStat = aConn.prepareStatement("UPDATE protein SET proteinid = ?, accession_number = ?, entry_name = ?, database_name = ?, species = ?, protein_pI = ?, protein_mw = ?, proteinlength = ?, creationdate = ?, modificationdate = CURRENT_TIMESTAMP, username = ? WHERE proteinid = ?");
		lStat.setLong(1, iProteinid);
		lStat.setObject(2, iAccession_number);
		lStat.setObject(3, iEntry_name);
		lStat.setObject(4, iDatabase_name);
		lStat.setObject(5, iSpecies);
		lStat.setObject(6, iProtein_pi);
		lStat.setObject(7, iProtein_mw);
		lStat.setLong(8, iProteinlength);
		lStat.setObject(9, iCreationdate);
		lStat.setObject(10, iUsername);
		lStat.setLong(11, iProteinid);
		int result = lStat.executeUpdate();
		lStat.close();
		this.iUpdated = false;
		return result;
	}


	/**
	 * This method allows the caller to insert the data represented by this
	 * object in a persistent store.
	 *
	 * @param   aConn Connection to the persitent store.
	 */
	public int persist(Connection aConn) throws SQLException {
		PreparedStatement lStat = aConn.prepareStatement("INSERT INTO protein (proteinid, accession_number, entry_name, database_name, species, protein_pI, protein_mw, proteinlength, creationdate, modificationdate, username) values(?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_USER)");
		if(iProteinid == Long.MIN_VALUE) {
			lStat.setNull(1, 4);
		} else {
			lStat.setLong(1, iProteinid);
		}
		if(iAccession_number == null) {
			lStat.setNull(2, 12);
		} else {
			lStat.setObject(2, iAccession_number);
		}
		if(iEntry_name == null) {
			lStat.setNull(3, -1);
		} else {
			lStat.setObject(3, iEntry_name);
		}
		if(iDatabase_name == null) {
			lStat.setNull(4, 12);
		} else {
			lStat.setObject(4, iDatabase_name);
		}
		if(iSpecies == null) {
			lStat.setNull(5, -1);
		} else {
			lStat.setObject(5, iSpecies);
		}
		if(iProtein_pi == null) {
			lStat.setNull(6, 3);
		} else {
			lStat.setObject(6, iProtein_pi);
		}
		if(iProtein_mw == null) {
			lStat.setNull(7, 3);
		} else {
			lStat.setObject(7, iProtein_mw);
		}
		if(iProteinlength == Long.MIN_VALUE) {
			lStat.setNull(8, 4);
		} else {
			lStat.setLong(8, iProteinlength);
		}
		int result = lStat.executeUpdate();

		// Retrieving the generated keys (if any).
		ResultSet lrsKeys = lStat.getGeneratedKeys();
		ResultSetMetaData lrsmKeys = lrsKeys.getMetaData();
		int colCount = lrsmKeys.getColumnCount();
		iKeys = new Object[colCount];
		while(lrsKeys.next()) {
			for(int i=0;i<iKeys.length;i++) {
				iKeys[i] = lrsKeys.getObject(i+1);
			}
		}
		lrsKeys.close();
		lStat.close();
		this.iUpdated = false;
		return result;
	}

	/**
	 * This method will return the automatically generated key for the insert if 
	 * one was triggered, or 'null' otherwise.
	 *
	 * @return	Object[]	with the generated keys.
	 */
	public Object[] getGeneratedKeys() {
		return this.iKeys;
	}

}